import requests,re

"""
新发地网站改版，网站源码没有数据源
"""
# 爬取电影天堂子页面中的下载地址

url = 'https://www.dytt89.com/'
resp = requests.get(url=url)
# 查看源码的charset 是什么，下边转码以下在输出出来
resp.encoding = 'gb2312'
# print(resp.text)

#编写预加载正则表达式
zz = re.compile('2023必看热片.*?<ul>(?P<ul>.*?)</ul>', re.S)
obj = re.compile("<a href='(?P<url>.*?)'", re.S)
obj3 = re.compile('◎片　　名(?P<name>.*?)<br />.*?<td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><a href="('
                  '?P<dyURL>.*?)"',re.S)
# 匹配热片区域的源码
resp1 = zz.finditer(resp.text)
child_href_list = []
# print(resp1)
print("---------------------------------------------------")
# 匹配源码里边的下载地址
for i in resp1:
    # print(i.group("ul"))
    ul = i.group('ul')
    # print("UL:", ul)

    result2 = obj.finditer(ul)
    # print("", result2)
    # 取出地址与域名拼接
    for ur in result2:
        # 去掉uri前边的斜杠
        chilr_link = url + ur.group('url').strip('/')
        # print(url+ur.group('url').strip('/'))
        # 子连接列表集合，遍历后放入
        child_href_list.append(chilr_link)
# print(child_href_list)

for href in child_href_list:
    # 遍历子连接集合，去get请求
    child_resp = requests.get(url=href)
    # 根据源码标记，进行编码
    child_resp.encoding = 'GB2312'
    # print(child_resp.text)
    # 匹配出电影的名称和下载地址
    sp3 = obj3.search(child_resp.text)
    print("电影名称+下载地址：", sp3.group(f'name', "dyURL"))
